﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyDoanVien.Bussiness;
using QuanLyDoanVien.Controller;

namespace QuanLyDoanVien
{
    public partial class frmQLBiThuChiDoan : DevComponents.DotNetBar.Office2007Form
    {
        public frmQLBiThuChiDoan()
        {
            InitializeComponent();
        }

        #region Bien
        List<Diem> dsDiem = new List<Diem>();
        HocSinhCtrl m_HocSinhCtrl = new HocSinhCtrl();
        List<HoatDongHocSinhInfo> dshdhs = new List<HoatDongHocSinhInfo>();
        List<BiThuChiDoanInfo> dsBiThu = new List<BiThuChiDoanInfo>();
        BiThuChiDoanCtrl m_BiThuChiDoanCtrl = new BiThuChiDoanCtrl();
        HoatDongHocSinhCtrl m_HoatDongHSCtrl = new HoatDongHocSinhCtrl();
        #endregion
        #region Ham
        private void loadDSDiem()
        {
            dsDiem.Clear();
            for (int i = 0; i <= 100; i += 10)
            {
                Diem d = new Diem();
                d.value = i;
                dsDiem.Add(d);
            }
        }
        private void loadDSBiThu()
        {
            dsBiThu = m_BiThuChiDoanCtrl.layDSBiThu();
            dataGridView_HS.Rows.Clear();
            foreach (BiThuChiDoanInfo btcd in dsBiThu)
            {
                dataGridView_HS.Rows.Add(btcd.MaHocSinh,btcd.HoTen,btcd.TenLop,btcd.GioiTinh,btcd.NgaySinh,btcd.NhanXet);
            }
            dataGridView_HS.Rows[0].Selected = true;
        }
        #endregion

        private void frmQLBiThuChiDoan_Load(object sender, EventArgs e)
        {
            loadDSDiem();
            loadDSBiThu();
        }

        private void btn_L_Sua_Click(object sender, EventArgs e)
        {
            if (dataGridView_HS.SelectedRows.Count < 1)
            {
                MessageBox.Show("Vui lòng chọn bí thư để cập nhật");
                return;
            }
            if (dataGridView_HS.SelectedRows[0].IsNewRow)
            {
                MessageBox.Show("Vui lòng chọn bí thư để cập nhật");
                return;
            }

            if (m_HocSinhCtrl.capNhatNhanXet(txt_HS_MaHs.Text, txt_HS_NhanXet.Text))
            {
                foreach (DataGridViewRow dr in dgvDiemBiThu.Rows)
                {
                    if (!dr.IsNewRow)
                    {
                        DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dr.Cells["DiemHD"];
                        m_HoatDongHSCtrl.capNhatHoatDongHS(txt_HS_MaHs.Text, dr.Cells["MaHoatDong"].Value.ToString(), cell.Value.ToString());
                    }
                }
                MessageBox.Show("Cập nhật bí thứ thành công");
                loadDSBiThu();
            }
            else
                MessageBox.Show("Cập nhật bí thư thất bại");
            
        }

        private void dataGridView_HS_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_HS.SelectedRows.Count > 0)
            {
                DataGridViewRow dr = dataGridView_HS.SelectedRows[0];
                if (dr.IsNewRow)
                    return;
                string maHS = dr.Cells["MaHS"].Value.ToString();
                dshdhs = m_HoatDongHSCtrl.layDSHDHS(maHS);
                dgvDiemBiThu.Rows.Clear();
                int i = 0;
                foreach (HoatDongHocSinhInfo hdHS in dshdhs)
                {
                    HoatDongCtrl hdCtrl = new HoatDongCtrl();
                    HoatDongInfo hd = hdCtrl.layHoatDong(hdHS.MaHoatDong);
                    dgvDiemBiThu.Rows.Add(hd.MaHoatDong,hd.TenHoatDong, null);
                    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dgvDiemBiThu.Rows[i].Cells["DiemHD"];
                    cell.ValueMember = "value";
                    cell.DisplayMember = "value";
                    cell.DataSource = dsDiem;
                    cell.Value = int.Parse(hdHS.MucDo);
                    i++;
                }
                txt_HS_MaHs.Text = dr.Cells["MaHS"].Value.ToString();
                txt_HS_NhanXet.Text = dr.Cells["NhanXet"].Value.ToString();
                txt_HS_TenHs.Text = dr.Cells["HoTen"].Value.ToString();
                txtGioiTinh.Text = dr.Cells["GioiTinh"].Value.ToString();
                txtMaLop.Text = dr.Cells["Lop"].Value.ToString();
                string date = dr.Cells["NgaySinh"].Value.ToString();
                string[] strs = date.Split('/');
                txt_HS_NS_Ngay.Text = strs[0];
                txt_HS_NS_Thang.Text = strs[1];
                txt_HS_NS_Nam.Text = strs[2];
                try
                {
                    picAvatar.Image = new Bitmap(frmQLHS.root_img_link + dsBiThu[dr.Index].HinhAnh);
                }
                catch
                {
                    picAvatar.Image = Properties.Resources.no_avatar;
                }
            }
        }

        private void txt_HS_Diem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void dgvDiemBiThu_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void bntRefesh_Click(object sender, EventArgs e)
        {
            loadDSDiem();
            loadDSBiThu();
        }

        
    }
}